package cn.songhaiqing.walle.help;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import cn.songhaiqing.walle.bean.HeartDaily;
import cn.songhaiqing.walle.bean.HeartProcess;
import cn.songhaiqing.walle.bean.HeartRecord;

public class HeartHelper {
    public static final String ACTION_HEART_SETTING_CHANGED = "com.gzgamut.smart_movement.ACTION_HEART_SETTING_CHANGED";
    public static final boolean DEF_HEART_ALARM = false;
    public static final int DEF_HEART_ALARM_HIGH = 200;
    public static final int DEF_HEART_ALARM_LOW = 30;
    public static final boolean DEF_HEART_ALARM_RING = true;
    public static final boolean DEF_HEART_ALARM_VIBRATION = true;
    public static final boolean DEF_HEART_SHOW = false;
    public static final String DEF_HREART_ALARM_RING_URL = "";
    public static final boolean DEF_LOST_MODE = false;
    private static final String KEY_HEART_ALARM_RING_URL = "KEY_HEART_ALARM_RING_URL";
    private static final String KEY_HEART_ALARM_VALUE_HIGH = "KEY_HEART_ALARM_VALUE_HIGH";
    private static final String KEY_HEART_ALARM_VALUE_LOW = "KEY_HEART_ALARM_VALUE_LOW";
    private static final String KEY_IS_BOUND_MODE_ON = "KEY_IS_BOUND_MODE_ON";
    private static final String KEY_IS_HEART_ALARM_ON = "KEY_IS_HEART_ALARM_ON";
    private static final String KEY_IS_HEART_ALARM_RING_ON = "KEY_IS_HEART_ALARM_RING_ON";
    private static final String KEY_IS_HEART_ALARM_VIBRATION_ON = "KEY_IS_HEART_ALARM_VIBRATION_ON";
    private static final String KEY_IS_HEART_SHOW_ON = "KEY_IS_HEART_SHOW_ON";
    private static final String KEY_IS_PHONE_MODE_ON = "KEY_IS_PHONE_MODE_ON";
    public static final int LEVEL_HIGH = 2;
    public static final int LEVEL_LOW = 0;
    public static final int LEVEL_NOMARL = 1;
    public static final int LEVEL_STRONG = 3;
    public static final String SPILT_VALUE = "_";
    public static final byte TYPE_ALARM = 6;
    public static final byte TYPE_FAIL = 1;
    public static final byte TYPE_FAIL_BY_BOUND = 0;
    public static final byte TYPE_GET_ALARM_MODE = 3;
    public static final byte TYPE_GET_MODE = 2;
    public static final byte TYPE_RECEIVE_ALARM_OPEN = 6;
    public static final byte TYPE_RECEIVE_SWITCH_STATE = 5;
    public static final byte TYPE_SET_ALAMR_MODE = 4;
    public static final byte TYPE_START_MODE = 0;
    public static final byte TYPE_START_MODE_BY_BOUND = 5;
    public static final byte TYPE_STOP_MODE = 1;
    public static final byte TYPE_SUCCESS = 0;
    public static final byte TYPE_SUCCESS_BY_BOUND = 1;

    public static void setIsHeartAlarmOn(boolean isOn) {
        SpHelper.putBoolean(KEY_IS_HEART_ALARM_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isOn);
    }

    public static void setIsHeartShow(boolean isshow) {
        SpHelper.putBoolean(KEY_IS_HEART_SHOW_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isshow);
    }

    public static void setIsBoundModeOn(boolean isOn) {
        SpHelper.putBoolean(KEY_IS_BOUND_MODE_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isOn);
    }

    public static boolean getIsBoundModeOn() {
        return SpHelper.getBoolean(KEY_IS_BOUND_MODE_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], false).booleanValue();
    }

    public static void setIsPhoneModeOn(boolean isOn) {
        SpHelper.putBoolean(KEY_IS_PHONE_MODE_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isOn);
    }

    public static boolean getIsPhoneModeOn() {
        return SpHelper.getBoolean(KEY_IS_PHONE_MODE_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], false).booleanValue();
    }

    public static boolean getIsHeartAlarmOn() {
        return SpHelper.getBoolean(KEY_IS_HEART_ALARM_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], false).booleanValue();
    }

    public static boolean getIsHeartShow() {
        return SpHelper.getBoolean(KEY_IS_HEART_SHOW_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], false).booleanValue();
    }

    public static void setIsHeartAlarmVibrationOn(boolean isOn) {
        SpHelper.putBoolean(KEY_IS_HEART_ALARM_VIBRATION_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isOn);
    }

    public static boolean getIsHeartAlarmVibrationOn() {
        return SpHelper.getBoolean(KEY_IS_HEART_ALARM_VIBRATION_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], true).booleanValue();
    }

    public static void setIsHeartAlarmRingOn(boolean isOn) {
        SpHelper.putBoolean(KEY_IS_HEART_ALARM_RING_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], isOn);
    }

    public static boolean getIsHeartAlarmRingOn() {
        return SpHelper.getBoolean(KEY_IS_HEART_ALARM_RING_ON + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], true).booleanValue();
    }

    public static void setHeartAlarmValueLow(int value) {
        SpHelper.putInt(KEY_HEART_ALARM_VALUE_LOW + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], value);
    }

    public static int getHeartAlarmValueLow() {
        return SpHelper.getInt(KEY_HEART_ALARM_VALUE_LOW + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], 30);
    }

    public static void setHeartAlarmValueHigh(int value) {
        SpHelper.putInt(KEY_HEART_ALARM_VALUE_HIGH + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], value);
    }

    public static int getHeartAlarmValueHigh() {
        return SpHelper.getInt(KEY_HEART_ALARM_VALUE_HIGH + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], 200);
    }

    public static void setHeartAlarmRingUrl(String url) {
        SpHelper.putString(KEY_HEART_ALARM_RING_URL + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], url);
    }

    public static String getHeartAlarmRingUrl() {
        return SpHelper.getString(KEY_HEART_ALARM_RING_URL + DeviceHelper.ARRAY_DEVICE[DeviceHelper.getDefaultDevice()], "");
    }

    public static int getLevel(int value) {
        if (value <= 50) {
            return 0;
        }
        if (value > 50 && value <= 100) {
            return 1;
        }
        if (value > 100 && value <= 160) {
            return 2;
        }
        if (value > 160) {
            return 3;
        }
        return 1;
    }

    public static HeartDaily getHeartDaily(HeartDaily heartDaily, int heart, int heart_temp, int index) {
        int max = heartDaily.getMax();
        int min = heartDaily.getMin();
        if (max < heart) {
            max = heart;
        }
        if (min > heart) {
            min = heart;
        }
        heartDaily.setMax(max);
        heartDaily.setMin(min);
        heartDaily.setAverage((int) (((double) ((float) (heart_temp / index))) + 0.5d));
        return heartDaily;
    }

    public static HeartDaily getMaxAverageMin_day(ArrayList<HeartProcess> heartProcessList) {
        String[] valueArray;
        HeartDaily heartDaily = new HeartDaily();
        int max = 0;
        int average = 0;
        int min = 999;
        int allValueCount = 0;
        if (heartProcessList == null || heartProcessList.size() <= 0) {
            heartDaily.setMax(0);
            heartDaily.setAverage(0);
            heartDaily.setMin(0);
        } else {
            Iterator<HeartProcess> it = heartProcessList.iterator();
            while (it.hasNext()) {
                for (String str : it.next().getHeart().split(SPILT_VALUE)) {
                    int temp = Integer.parseInt(str);
                    if (max < temp) {
                        max = temp;
                    }
                    if (min > temp) {
                        min = temp;
                    }
                    average += temp;
                    allValueCount++;
                }
            }
            heartDaily.setMax(max);
            heartDaily.setAverage((int) (((double) ((float) (average / allValueCount))) + 0.5d));
            heartDaily.setMin(min);
        }
        return heartDaily;
    }

    public static HeartDaily getMaxAverageMin_day(ArrayList<HeartProcess> heartProcessList, int index_click) {
        String[] valueArray;
        HeartDaily heartDaily = new HeartDaily();
        int max = 0;
        int average = 0;
        int min = 999;
        int allValueCount = 0;
        if (heartProcessList == null || heartProcessList.size() <= 0) {
            heartDaily.setMax(0);
            heartDaily.setAverage(0);
            heartDaily.setMin(0);
        } else {
            for (String str : heartProcessList.get(index_click).getHeart().split(SPILT_VALUE)) {
                int temp = Integer.parseInt(str);
                if (max < temp) {
                    max = temp;
                }
                if (min > temp) {
                    min = temp;
                }
                average += temp;
                allValueCount++;
            }
            heartDaily.setMax(max);
            heartDaily.setAverage((int) (((double) ((float) (average / allValueCount))) + 0.5d));
            heartDaily.setMin(min);
        }
        return heartDaily;
    }

    public static HeartDaily getMaxAverageMin_day(HeartProcess heartProcess) {
        String[] valueArray;
        HeartDaily heartDaily = new HeartDaily();
        int max = 0;
        int average = 0;
        int min = 999;
        int allValueCount = 0;
        if (heartProcess != null) {
            for (String str : heartProcess.getHeart().split(SPILT_VALUE)) {
                int temp = Integer.parseInt(str);
                if (max < temp) {
                    max = temp;
                }
                if (min > temp) {
                    min = temp;
                }
                average += temp;
                allValueCount++;
            }
            heartDaily.setMax(max);
            heartDaily.setAverage((int) (((double) ((float) (average / allValueCount))) + 0.5d));
            heartDaily.setMin(min);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(heartProcess.getDate());
            heartDaily.setDate(calendar);
        } else {
            heartDaily.setMax(0);
            heartDaily.setAverage(0);
            heartDaily.setMin(0);
        }
        return heartDaily;
    }

    public static HeartRecord getHeartRecord(HeartProcess heartProcess) {
        String[] valueArray;
        HeartRecord heartRecord = new HeartRecord();
        int max = 0;
        int average = 0;
        int min = 999;
        int allValueCount = 0;
        int lightSecond = 0;
        int fatSecond = 0;
        int aerobicSecond = 0;
        int intenseSecond = 0;
        int peakSecond = 0;
        int largest = 0;
        if (heartProcess != null) {
            for (String str : heartProcess.getHeart().split(SPILT_VALUE)) {
                int temp = Integer.parseInt(str);
                if (max < temp) {
                    max = temp;
                }
                if (min > temp) {
                    min = temp;
                }
                if (temp > 154) {
                    peakSecond++;
                } else if (temp > 135) {
                    intenseSecond++;
                } else if (temp > 116) {
                    aerobicSecond++;
                } else if (temp > 97) {
                    fatSecond++;
                } else {
                    lightSecond++;
                }
                average += temp;
                allValueCount++;
            }
            if (lightSecond > 0) {
                largest = lightSecond;
            }
            if (fatSecond > largest) {
                largest = fatSecond;
            }
            if (aerobicSecond > largest) {
                largest = aerobicSecond;
            }
            if (intenseSecond > largest) {
                largest = intenseSecond;
            }
            if (peakSecond > largest) {
                largest = peakSecond;
            }
            heartRecord.setMax(max);
            heartRecord.setAverage((int) (((double) ((float) (average / allValueCount))) + 0.5d));
            heartRecord.setMin(min);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(heartProcess.getDate());
            heartRecord.setDate(calendar);
            heartRecord.setLightSecond(lightSecond);
            heartRecord.setFatSecond(fatSecond);
            heartRecord.setAerobicSecond(aerobicSecond);
            heartRecord.setIntenseSecond(intenseSecond);
            heartRecord.setPeakSecond(peakSecond);
            heartRecord.setLargest(largest);
        } else {
            heartRecord.setMax(0);
            heartRecord.setAverage(0);
            heartRecord.setMin(0);
        }
        return heartRecord;
    }
}
